<?php
  // liquidsoap.inc
  // Dial Radio Control System
  //     Liquidsoap interop functions
  // June 10, 2010

class Liquidsoap {
    
    private static function sendCommand($command) {
        $fp = fsockopen(DIAL_LIQUIDSOAP_SOCK, DIAL_LIQUIDSOAP_TIMEOUT, $errorno, $error);
        if($fp === false) {
            throw new Exception("Can't connect to Liquidsoap!");
        } else {
            fwrite($fp, "{$command}\nexit\n");
            while (!feof($fp)) {
                $return .= fgets($fp, 128);
            }
            fclose($fp);
        }
        return strstr($return, 'END', true);
    }
    
    public static function isUp() {
        try {
            self::sendCommand('uptime');
        } catch (Exception $e) {
            return false;
        }
        return true;
    }
    
    public static function uptime() {
        return self::sendCommand('uptime');        
    }
}